package com.bbs.controller;

import com.bbs.entity.BBSPostCategory;
import com.bbs.service.IBBSPostCategoryService;
import com.bbs.service.IBBSPostService;
import com.bbs.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: com.bbs.controller
 * @ClassName: IndexCtrooler
 * @Description:
 * @Author: Rambo
 * @Data: 2024/6/19 10:55
 */
@Controller
public class IndexController {
    @Autowired
    private IBBSPostService bbsPostService;
    @Autowired
    private IBBSPostCategoryService bbsPostCategoryService;

    /**
     * 首页
     * @param request
     * @param categoryId
     * @param page
     * @param keyword
     * @param period
     * @param orderBy
     * @return
     */
    @GetMapping({"", "/", "/index", "/index.html"})
    public String IndexPage(HttpServletRequest request,
                            @RequestParam(value = "categoryId", required = false) Long categoryId,
                            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                            @RequestParam(value = "keyword", required = false) String keyword,
                            @RequestParam(value = "period", required = false) String period,
                            @RequestParam(value = "orderBy", required = false) String orderBy) {
        // 获取帖子分类类别信息
        List<BBSPostCategory> bbsPostCategories = bbsPostCategoryService.getBBSPostCategories();
        // 判断类别信息是否为空
        if (CollectionUtils.isEmpty(bbsPostCategories)) {
            return "error/error_404";
        }

        // 将分类数据封装到request域中
        request.setAttribute("bbsPostCategories", bbsPostCategories);

        // 将近期热议的帖子数据封装到request域中
        request.setAttribute("hotTopicBBSPostList", bbsPostService.getHotTopicBBSPostList());

        //封装参数
        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);//默认每页10条
        params.put("start", (page - 1) * 10);
        //帖子类别
        if (categoryId != null && categoryId > 0) {
            request.setAttribute("categoryId", categoryId);
            params.put("categoryId", categoryId);
        }
        //搜索关键字
        if (!StringUtil.isEmpty(keyword)) {
            request.setAttribute("keyword", keyword);
            params.put("keyword", keyword);
        }
        //时间周期 (周榜、月榜、全部)
        if (!StringUtil.isEmpty(period)) {
            request.setAttribute("period", period);
            params.put("period", period);
        }
        //排序（按照阅读量或者时间排序）
        if (!StringUtil.isEmpty(orderBy)) {
            request.setAttribute("orderBy", orderBy);
            params.put("orderBy", orderBy);
        }

        // 调用业务层方法进行分页查询首页帖子列表并存储到request域中
        request.setAttribute("pageResult", bbsPostService.getBBSPostPageForIndex(params));

        return "index";
    }
}
